<?php
	include('dbconnect.php');
	require_once('class.Fitness.php');
	require_once('class.UserDB.php');
  	
	$fitness = new Fitness();
	$userDB = new UserDB();
    /*$xmlData = file_get_contents("http://www.ashkansotoudeh.com/blog/?feed=rss2");
	$xmlData = str_replace("content:encoded","content",$xmlData);
	$xmlData = str_replace("dc:creator","creator",$xmlData);
	$xml = new SimpleXMLElement($xmlData);
	$item = $xml->channel;*/
	if (isset($_SESSION['fName']) ? $user = $_SESSION['fName'] : $user = "Guest");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<script type="text/javascript" >
function loadercheck() {
  // display loading image here...
  document.getElementById("loadingImg").style.display = "block";
  document.getElementById("content").style.display = 'none';
  // request your data...
  var req =  new XMLHttpRequest();
  req.open("GET", "search.php?s=null", true);
  req.send();
  req.onreadystatechange = function(){
  	if (req.readyState == 4 && req.status == 200) {
  		// content is loaded...hide the gif and display the content...
				if (req.responseText) {
					//document.getElementById("content").innerHTML = "<img src=\"/images/load.gif\" alt=\"...\" />";
					document.getElementById("content").style.display = 'block';
					document.getElementById("loadingImg").style.display = "none";
				}
			}
		}
}
</script>
	<link rel="Icon" href="favicon.ico" />
    <link rel="Stylesheet" type="text/css" media="screen" href="style.css" />
    <title><?php $config->displayHeader(); ?></title>
</head>
<body onload="loadercheck()">
	<a name="top"></a>
    <div id="header">
		<div id="navigation">
    	<a id="navitem" href="index.php">Home</a> |
    	<a id="navitem" href="codeSnippets.php">Code Snippets</a> |
		<a id="navitem" href="/blog">Blog</a> |
    	<a id="navitem" href="rss.php">RSS Feed</a> |
		<a id="navitem" href="about.php">About Me</a> |
		<?php if (!isset($_SESSION['fName'])) { ?>
			<a id="navitem" href="login.php">Login</a>
		<?php } else { ?>
			<a id="navitem" href="admin.php">Admin</a>
		<?php } ?>
		</div>
		<h1 class="title"><a class="title" href="index.php"><?php $config->displayHeader(); ?></a></h1>
		<h3 class="quote"><?php $config->displayQuote(1); ?></h3>
	</div>
	<div id="welcome"><?php echo "Welcome, " . $user ?></div>
	<div id="sidebar">Recent Posts
		<?php
			$query = $db->get("SELECT COUNT(*) FROM wp_posts WHERE post_status='publish' AND post_type='post'");
			if (!$query) {
				echo "Could not run query: " . $db->error;
				exit;
			}
			$postCount = $query->fetch_row();
			echo " ($postCount[0])";
		?>
		<div id="sidebardata">
		<?php
			$query = $db->get("SELECT * FROM wp_posts WHERE post_status='publish' AND post_type='post' ORDER BY post_date DESC");
			if (!$query) {
				echo "Could not run query: " . $db->error;
				exit;
			}
			while ($row = mysqli_fetch_array($query)) {
				$id = $row['ID'];
				$title = $row['post_title'];
				echo "<a id='sidebardata' href='#$id'>" . $title . "</a><br \>";
		}
		?>
		</div>
	</div>
	<div id="sidebar2">Links
		<div id="sidebardata">
			<a id='sidebardata' href="http://cs.senecac.on.ca/" target="_blank">CS Seneca</a><br />
			<a id='sidebardata' href="http://my.senecacollege.ca/" target="_blank">My Seneca</a><br />
			<a id='sidebardata' href="https://learn.senecac.on.ca/" target="_blank">Learn Email</a><br />
			<a id='sidebardata' href="http://cdot.senecac.on.ca/" target="_blank">CDOT</a><br />
			<a id='sidebardata' href="http://webchat.freenode.net?channels=seneca&uio=d4" target="_blank">Freenode IRC</a>
			</div>
	</div>
    <div id="content">
	<img id="loadingImg" src="images/load.gif" alt="loading" />
    <?php
		if (isset($_POST['btn_Submit'])) {
			if($userDB->login($_POST['txtUser'],$_POST['txtPasswd'])) {
				$uinfo = $userDB->getUserInfo($_POST['txtUser']);
				$_SESSION['fName'] = $uinfo[3];
    		}
		}
		?>
			<div id='heading'>Recent Blog Posts</div>
			<br />
		<?php
			$query = $db->get("SELECT * FROM wp_posts WHERE post_status='publish' AND post_type='post' ORDER BY post_date DESC");
			if (!$query) {
				echo "Could not run query: " . $db->error;
				exit;
			}	
			while ($row = mysqli_fetch_array($query)) {
				// convert date/time to human-readable format
				$tempDate = strtotime($row[2]);
				$date = date("F j, Y, g:i a", $tempDate);

				$content = $row['post_content'];
				$content = str_replace("<?php","&lt;?php",$content);
				$content = str_replace("?>","?&gt;",$content);
				
				$display->displayPost($row['ID'], $row['post_title'], $content, $date, "Ashkan");
			}
?>
    </div>
	
	<div id="footer">
    	<a href="#">About</a> - <a href="#">Contact Us</a> - <a href="#">Help</a>
    	<br />
    	<?php $config->displayFooter(); ?>
    </div>
</body>
</html>
